package com.project.yunshitong;

import static com.project.yunshitong.Utils.getToast;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.seuic.uhf.UHFService;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView ivBack;
    private TextView tvTitle;
    private EditText etPower;
    private TextView tvSetting;
    private Context context;

    private UHFService UTouchUHF;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        context = this;
        initView();
        InitRfid();
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        tvTitle = findViewById(R.id.tvTitle);
        etPower = findViewById(R.id.etPower);
        tvSetting = findViewById(R.id.tvSetting);
        tvTitle.setText("设置功率");
        ivBack.setOnClickListener(this);
        tvSetting.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvSetting:
                if (etPower.getText().toString().trim().isEmpty()) {
                    getToast(this, "请输入要设置的功率！");
                    return;
                }
                RfidSetPower(Integer.parseInt(etPower.getText().toString().trim()));
                break;
        }
    }


    //    初始化AUTOID UTouch(c)手持机
    public void InitRfid() {
        try {
            UTouchUHF = UHFService.getInstance(this);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("初始化设备结果", "初始化设备");
//            Toast.makeText(context, R.string.Initialization_failure, Toast.LENGTH_SHORT).show();
            return;
        }
        if (UTouchUHF != null && UTouchUHF.open()) {
            byte[] embd = new byte[255];
            embd[0] = (byte) 2;
            embd[1] = (byte) 0;
            embd[2] = (byte) 12;
//            System.arraycopy(getHexByteArray(et_embeded_acpwd.getText().toString()), 0, embd, 3, 4);
            boolean IsSet = UTouchUHF.setParamBytes(UHFService.PARAMETER_TAG_EMBEDEDDATA, embd);
//            设置隐藏PC号
            boolean IsSetPC = UTouchUHF.setParameters(UHFService.PARAMETER_HIDE_PC, 1);
//            设置读取速度
            UTouchUHF.setParameters(UHFService.PARAMETER_INVENTORY_SPEED, 0);
            boolean IsSESSION = UTouchUHF.setParameters(UHFService.PARAMETER_INVENTORY_SESSION, 0);
            boolean IsProfile = UTouchUHF.setParameters(UHFService.PARAMETER_LINK_PROFILE, 1);
            Log.e("是否设置成功", "" + IsSESSION + ",隐藏PC" + IsSetPC + ",设置附加数据" + IsSet + ",灵敏度" + IsProfile);
            etPower.setText(RfidGetPower() + "");
        }
    }

    // 设置AUTOID UTouch(c)手持机功率
    public void RfidSetPower(int Power) {
        ///东集手持机
        if (UTouchUHF != null) {
            boolean IsSet = false;
            // 设置功率
            IsSet = UTouchUHF.setPower(Power);
            if (IsSet) {
                getToast(context, "设置成功！");
            } else {
                getToast(context, "设置失败！");
            }
            Log.e("设置功率:", "" + IsSet);
        }
    }

    // 查询AUTOID UTouch(c)手持机功率
    int Power = 0;

    public int RfidGetPower() {
        //、东集手持机
        if (UTouchUHF != null) {
            Power = UTouchUHF.getPower();
            Log.e("模块功率:", "" + Power);
            return Power;
        } else {
            return 0;
        }
    }


}